﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;



namespace CRM.Tools.JavaScriptConverter
{
    /// <summary>
    /// Convert CRM 4 JavaScript to CRM 2011 version 
    /// </summary>
    public partial class ConvertWindow : Window
    {
        public ConvertWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Trigger conversion
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uxConvert_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                uxConvert.IsEnabled = false;

                if(String.IsNullOrEmpty(uxCRM4.Text))
                {
                    MessageBox.Show("You have not provided any Javascript in the CRM4 Script textbox.");
                    return;
                }
                
                // Open the Conversion xml file and retrieve the snippet nodes
                XmlDocument doc = ConversionHelper.OpenConversionList("ConversionList.xml");
                XmlNodeList converisionItems = doc.SelectNodes("/snippets/snippet");
                if (converisionItems.Count == 0)
                {
                    MessageBox.Show("No conversion items have been found in the Conversion List file. Ensure the Conversion List contains at least one snippet.");
                    return;
                }

                // Perform the conversion
                uxCRM5.Text = ConversionHelper.ConvertScript(uxCRM4.Text, converisionItems);

            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred during the conversion process. " + ex.Message);
            }
            finally
            {
                uxConvert.IsEnabled = true;
            }
        }

        /// <summary>
        /// Display about box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uxAboutMenu_Click(object sender, RoutedEventArgs e)
        {
            AboutBox1 about = new AboutBox1();
            about.ShowDialog();
        }

        /// <summary>
        /// Exit Application
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uxFileMenuExit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void uxSaveAs_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Configure save file dialog box
                Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
                dlg.FileName = "Untitled"; // Default file name
                dlg.DefaultExt = ".js"; // Default file extension
                dlg.Filter = "JScript files (.js)|*.js"; // Filter files by extension

                // Show save file dialog box
                Nullable<bool> result = dlg.ShowDialog();

                // Process save file dialog box results
                if (result == true)
                {
                    // Save document
                    string filename = dlg.FileName;

                    System.IO.File.AppendAllText(filename, uxCRM5.Text);
                    MessageBox.Show("File Saved Successfully");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("File Not Saved");
            }
        }


        private void uxCRM4_Cut_Click(object sender, RoutedEventArgs e)
        {

            uxCRM4.Cut();
        }

        private void uxCRM4_Copy_Click(object sender, RoutedEventArgs e)
        {

            uxCRM4.Copy();
        }

        private void uxCRM4_Paste_Click(object sender, RoutedEventArgs e)
        {

            uxCRM4.Paste();
        }

        private void uxCRM4_Clear_Click(object sender, RoutedEventArgs e)
        {

            uxCRM4.Clear();
        }

        private void uxCRM4_SelectAllText_Click(object sender, RoutedEventArgs e)
        {
            uxCRM4.SelectAll();
        }

        private void uxCRM4_Undo_Click(object sender, RoutedEventArgs e)
        {
            uxCRM4.Undo();
        }

        private void uxCRM5_Cut_Click(object sender, RoutedEventArgs e)
        {

            uxCRM5.Cut();
        }

        private void uxCRM5_Copy_Click(object sender, RoutedEventArgs e)
        {

            uxCRM5.Copy();
        }

        private void uxCRM5_Paste_Click(object sender, RoutedEventArgs e)
        {

            uxCRM5.Paste();
        }

        private void uxCRM5_Clear_Click(object sender, RoutedEventArgs e)
        {

            uxCRM5.Clear();
        }

        private void uxCRM5_SelectAllText_Click(object sender, RoutedEventArgs e)
        {
            uxCRM5.SelectAll();
        }

        private void uxCRM5_Undo_Click(object sender, RoutedEventArgs e)
        {
            uxCRM5.Undo();
        }
    }
}
